<%--
    Document   : index
    Created on : Mar 5, 2013, 8:58:23 AM
    Author     : ThanhTai
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title>News</title>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/xmlproject.js"></script>
    </head>
    <script type="text/javascript">
        var allNewsDOM=null;
        var newsXMLDOM=null;

        function getAllNews(filename){
            loadXML(filename, function complete(result) {
                allNewsDOM = result;
            });
        }

        function returnAllNewsDom(){
            return allNewsDOM;
        }

        getAllNews('xml/news.xml');

        function handleEnterName(e){
            var charCode = (e.which || e.which == 0) ? e.which : event.keyCode //e.which==0 is for firefox
            if (charCode > 32 && (charCode < 48 ||
                (charCode > 57 && charCode < 65) ||
                (charCode > 90 && charCode < 97) || charCode > 122)  )
                return false;
            return true;
        }
        
        
        function getHttpRequest(){
            try{
                //ff
                //xmlDom = new XMLHttpRequest();
                allNewsDOM= document.implementation.createDocument("","",null);
            }
            catch(e){
                try{
                    allNewsDOM = new ActiveXObject("Msxm12:XMLHTTP");
                }
                catch(e){
                    try{
                        allNewsDOM = new ActiveXObject("Microsoft.XMLHTTP")
                    }catch(e){
                        alert(e);
                    }
                }
            }
        }
        
        function convertText(doc){
            var xmlContent;
            try{
                if(window.ActiveXObject){
                    xmlContent = doc.documentElement.xml;
                }else{
                    xmlContent = new XMLSerializer().serializeToString(doc);
                }
                return xmlContent;
            }catch(e){
                return "";
                e.toString();
            }
        }


        function combineXML_XSL(xml,xsl){
            if (window.ActiveXObject) {
                var out = xml.transformNode(xsl);
                document.getElementById("newsBody").innerHTML = out;
            } else {
                var xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(xsl);
                resultDocument = xsltProcessor.transformToFragment(xml, document);
                document.getElementById("newsBody").innerHTML= "";
                document.getElementById("newsBody").innerHTML=   convertText(resultDocument);
            }
        }
        function SearchNews(){
            try{
                allNewsDOM=returnAllNewsDom();
                if (allNewsDOM==null){
                    allNewsDOM("xml/news.xml");
                }
                if (allNewsDOM==null){
                    return;
                }

                var xsl =document.implementation.createDocument("","",null);
                xsl.async=false;
                xsl.load('xsl/news.xsl');

                var name = document.getElementById("txtSearchNews").value.trim().replace(/\s{2,}/g, ' ');
                
                if((name.length==0)){
                    combineXML_XSL(allNewsDOM,xsl);
                    return;
                }
                
                var str="//new[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"+name.toLowerCase()+
                    "') or contains(translate(header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'"+name.toLowerCase()+
                    "')]";
                var result=allNewsDOM.evaluate(str, allNewsDOM,null, XPathResult.ANY_TYPE , null);

                var param = result.iterateNext();
                if(param==null){
                    document.getElementById("newsBody").innerHTML= "";
                    return;
                }
                else{
                    newsXMLDOM = "<news>";
                    while (param) {
                        newsXMLDOM+="<new>";
                        newsXMLDOM+="<ID>"+ param.getElementsByTagName("ID")[0].childNodes[0].nodeValue+"</ID>";
                        newsXMLDOM+="<title>"+param.getElementsByTagName("title")[0].childNodes[0].nodeValue+"</title>";
                        newsXMLDOM+="<image>"+param.getElementsByTagName("image")[0].childNodes[0].nodeValue+"</image>";
                        newsXMLDOM+="<header>"+ param.getElementsByTagName("header")[0].childNodes[0].nodeValue+"</header>";
                        newsXMLDOM+="<closing>"+ param.getElementsByTagName("closing")[0].childNodes[0].nodeValue+"</closing>";
                        newsXMLDOM+="<date>"+ param.getElementsByTagName("date")[0].childNodes[0].nodeValue+"</date>";
                        newsXMLDOM+="<status>"+ param.getElementsByTagName("status")[0].childNodes[0].nodeValue+"</status>";
                        newsXMLDOM+="<newDetail>";
                        newsXMLDOM+="<newDetailID>"+param.getElementsByTagName("newDetailID")[0].childNodes[0].nodeValue+"</newDetailID>";
                        newsXMLDOM+="<newID>"+ param.getElementsByTagName("newID")[0].childNodes[0].nodeValue+"</newID>";
                        newsXMLDOM+="<paragrap>"+ param.getElementsByTagName("paragrap")[0].childNodes[0].nodeValue+"</paragrap>";
                        newsXMLDOM+="</newDetail>";
                        newsXMLDOM+="</new>";
                        param = result.iterateNext();
                    }
                    newsXMLDOM+="</news>";
                    var parser = new DOMParser();
                    newsXMLDOM = parser.parseFromString(newsXMLDOM, "text/xml");
                    newsXMLDOM.async=false;
                    combineXML_XSL(newsXMLDOM,xsl);
                }
            }
            catch(e){alert(e);
                return;
            }
        }
        // mouse event
        
    </script>
    <body onload="loadCart();  rotateBanner(0);">
        <div id="wrapper">
            <jsp:include page="indexHeader.jsp"></jsp:include>
            <div id="body">
                <div id="tabletNews">
                    <div class="clear"></div>
                    <h2 style="color:#000000" align="center">
                        Tablet News
                    </h2>
                    <div class="searchNews">
                        <table border="0" style="background-color: white">
                            <tbody>
                                <tr>
                                    <td>
                                        <div style="margin-right: 10px; font-size: 13pt; font-style: oblique">Search by title and header: </div>
                                    </td>
                                    <td>
                                        <div>
                                            <input type="text" name="txtSearchNews" id ="txtSearchNews" value=""
                                                    maxlength="1000"/>
                                        </div>
                                    </td>
                                    <td width="20px" align="center">
                                        <div id="button-search">
                                            <input type="submit" value="Search" onclick="SearchNews();" name="btnSearch"/>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>


                    <div id="newsBody">
                        <c:import charEncoding="UTF-8" url="xml/news.xml" var="xmldoc"/>
                        <c:import charEncoding="UTF-8" url="xsl/news.xsl" var = "xsldoc"/>
                        <x:transform xml="${xmldoc}" xslt="${xsldoc}"/>
                    </div>
                </div><!--End body-->
            </div><!--End body-->
            <div id="fotter">
                <center>
                    <span> ﻿© 2023 by FPT_University. All rights reserved.</span></center>
            </div><!--End fotter-->
        </div><!--End wrapper-->

    </body>
</html>
